<?php
/*
* Author: Nguyen Hai Nam
* Module: Config_CompanyModule
* Desc	: Introduce pick.vn and sidle pic
* Date	: 12/12/2011
*/
class Config_CompanyModule extends Pick_Core_Module{
	
	public $authentication = 1;
	
	function Config_CompanyModule(){
		
		parent::Pick_Core_Module();
		$this->theme = "admin";
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		
		$act = $this->request->element('act', "list");
		$this->template->assign('act', $act);
		if($act == "position"){
			$type 	= new Pick_Core_Type();
			$oId = $this->request->element('id','');
			$val = $this->request->element('val','');
			$arrData = array('position'=>$val);
			echo $type->updateData($arrData, $oId);
			die;
		}
		if($act == "add"){
			$this->addAction();
		}else{
			$this->listAction();	
		}
		
	}
	public function listAction(){
		
		$configs 	= new Pick_Core_Configs();
		$upload     = new Pick_Core_Upload();
		
		$infoText   = '';
		$infoClass  = "InfoText";
		$avatar     = isset($_FILES['avatar'])?$_FILES['avatar']:'';
		
		$config = $configs->getObject("company");
		
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
						
		if($_POST) {
		
			# Validation
			$validate 	= new Pick_Data_Validate();
			$validatemess = $this->validateData($this->request, $validate);
			if($validatemess == '') {
				foreach($objectLanguages as $objLang){
					$nameCompany = $objLang->getField("code")."_company";
					$nameAddress = $objLang->getField("code")."_address";
					$config->setProperty($nameCompany,$this->request->element($nameCompany));
					$config->setProperty($nameAddress,$this->request->element($nameAddress));
				}
				$config->setProperty('tel',$this->request->element('tel'));
				$config->setProperty('fax',$this->request->element('fax'));
				$config->setProperty('hotline',$this->request->element('hotline'));
				$config->setProperty('admin_mail',$this->request->element('admin_mail'));		
				
				# Duong dan de upload hinh logo
				$gallery = ROOT_PATH."upload/images/";
				$dirnews = $gallery."logocp/";
				$gallery_storage = $dirnews."storage";
							
				# X�a hinh neu nguoi dung chon
				$del_images = $this->request->element('del_images', 0);
				if($del_images){
					unlink($gallery_storage.'/'.$config->getProperty('avatar'));
					$config->setProperty('avatar',"");
				} #endif        
				
				/* Duong dan */
				if($avatar){
		
					$avatarUpload = new Pick_Core_Upload($avatar);
					$copy   = $avatarUpload->moveFile($gallery_storage);
					if(!$copy){ 
						$validatemess .= $this->l('cannotuploadfile') . $this->l('check_type');
					}else{
						// Delete logo old before upload new logo
						unlink($gallery_storage.'/'.$config->getProperty('avatar'));
						//Upload new logo
						$config->setProperty('avatar',$avatarUpload->getNameFile());
					}             
					
				}  		
				
				if($configs->updateData($config)) {
					$infoText = $this->l('update_config_ok');
					$this->template->assign("InfoText",$infoText);
					$this->template->assign("infoClass",$infoClass);
				} else {
					$infoText = $messages['update_config_error']."<br />".$validatemess;
					$infoClass = "infoText";
					$this->template->assign("InfoText",$infoText);
					$this->template->assign("infoClass",$infoClass);
				}
			} else {
				$infoText = $this->l('update_config_error');
				$infoClass = "infoText";	
				$this->template->assign("InfoText",$infoText);				
			}
		}
		foreach($objectLanguages as $objLang){
			$nameCompany = $objLang->getField("code")."_company";
			$nameAddress = $objLang->getField("code")."_address";
			$this->template->assign($nameCompany,$config->getProperty($nameCompany));
			$this->template->assign($nameAddress,$config->getProperty($nameAddress));
		}
		$addressDefault = DEFAULT_LANGUAGE."_address";
		$this->template->assign('addressDefault',$config->getProperty($addressDefault));
		
		$this->template->assign("objectLanguages", $objectLanguages);	

		$tel=$config->getProperty('tel'); 
		$this->template->assign("tel",$tel);
		
		$fax=$config->getProperty('fax'); 
		$this->template->assign("fax",$fax);
		
		$hotline=$config->getProperty('hotline'); 
		$this->template->assign("hotline",$hotline);
		
		$admin_mail=$config->getProperty('admin_mail'); 
		$this->template->assign("admin_mail",$admin_mail);
		
		$coordinates_x=$config->getProperty('coordinates_x'); 
		$this->template->assign("coordinates_x",$coordinates_x);
		
		$coordinates_y=$config->getProperty('coordinates_y'); 
		$this->template->assign("coordinates_y",$coordinates_y);
		
		$coordinates = '('.$coordinates_x.','.$coordinates_y.')';
		$this->template->assign("coordinates",$coordinates);
		
		$nameFileLogo=$config->getProperty('avatar'); 
		$this->template->assign("nameFileLogo",$nameFileLogo);
		$this->template->assign("avatar","<img src='".base_url()."/upload/images/logocp/storage/".$nameFileLogo."'/>");
		
		$this->template->assign("infoText",$infoText);
		$this->template->assign("infoClass",$infoClass);

	}
	
	public function addAction(){

	}
	
	function validateData($request, $validate) {	
		return "";
	}
}